﻿using Common.Communication;
using Common.Tool.DataTools;
using DeviceBase.Enums;
using DeviceBase.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DianZiFuZai.Core.Itech
{
    public class IT8813NiVisa : NiVisaBase, IDianZiFuZai
    {
        public string Name { get; set; }
        public IT8813NiVisa(string name, string sn, int timeout = 1500) : base(sn, timeout)
        {
            Name = name;
        }
        public Result<double> ReadCurrentValue(DianZiFuZaiEnum.FuZaiMode mode)
        {
            string command = "";
            double value = 0f;
            Result<double> result = new Result<double>();
            AppendInfo(Name, $"读取当前负载{mode.ToString()}");
            command = CmdReadCurrentValue(mode);
            //发送指令并返回
            var re2 = WriteThenRead(command);
            if (!re2.IsSucceed || !double.TryParse(re2.Value, out value))
            {
                result.IsSucceed = false;
                result.Err = "读取当前负载失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            result.Value = value;
            if (mode == DianZiFuZaiEnum.FuZaiMode.电压)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "V");
            }
            if (mode == DianZiFuZaiEnum.FuZaiMode.电流)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "A");
            }
            if (mode == DianZiFuZaiEnum.FuZaiMode.功率)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "W");
            }
            if (mode == DianZiFuZaiEnum.FuZaiMode.电阻)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "Ω");
            }
            return result.EndTime();
        }

        public Result<double> ReadSetValue(DianZiFuZaiEnum.FuZaiMode mode)
        {
            string command = "";
            double value = 0f;
            Result<double> result = new Result<double>();
            AppendInfo(Name, $"读取设置负载{mode.ToString()}");
            command = CmdReadFuZaiValue(mode);
            //发送指令并返回
            var re2 = WriteThenRead(command);
            if (!re2.IsSucceed || !double.TryParse(re2.Value, out value))
            {
                result.IsSucceed = false;
                result.Err = "读取设置负载失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            result.Value = value;
            if (mode == DianZiFuZaiEnum.FuZaiMode.电压)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "V");
            }
            if (mode == DianZiFuZaiEnum.FuZaiMode.电流)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "A");
            }
            if (mode == DianZiFuZaiEnum.FuZaiMode.功率)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "W");
            }
            if (mode == DianZiFuZaiEnum.FuZaiMode.电阻)
            {
                AppendResult(Name, DataConvert.ToUnit(result.Value, "F3") + "Ω");
            }
            return result.EndTime();
        }

        public Result<bool> SetFuZai(DianZiFuZaiEnum.FuZaiMode mode, double value)
        {
            string command = "";
            Result<bool> result = new Result<bool>();
            AppendInfo(Name, $"设置负载{mode.ToString()}{value}");
            command = CmdSetFuZaiValue(mode,value);
            var re = Write(command);
            if (!re.IsSucceed)
            {
                result.IsSucceed = false;
                result.Err = "设置负载失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            Thread.Sleep(100);
            command = CmdReadFuZaiValue(mode);
            //发送指令并返回
            double va = 0;
            var re2 = WriteThenRead(command);
            if (!re2.IsSucceed || !double.TryParse(re2.Value, out va) || va != value)
            {
                result.IsSucceed = false;
                result.Err = "设置负载失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            AppendInfo(Name, "设置负载成功");
            return result.EndTime();
        }

        public Result<bool> SetFuZaiMode(DianZiFuZaiEnum.FuZaiMode mode)
        {
            string command = "";
            Result<bool> result = new Result<bool>();
            AppendInfo(Name, $"设置负载模式{mode.ToString()}");
            command = CmdSetFuZaiMode(mode);
            var re = Write(command);
            if (!re.IsSucceed)
            {
                result.IsSucceed = false;
                result.Err = "设置负载模式失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            Thread.Sleep(100);
            command = CmdReadFuZaiMode;
            //发送指令并返回
            var re2 = WriteThenRead(command);
            if (!re2.IsSucceed || !re2.Value.Contains(CmdReadFuZaiModeOk(mode)))
            {
                result.IsSucceed = false;
                result.Err = "设置负载模式失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            AppendInfo(Name, "设置负载模式成功");
            return result.EndTime();
        }


        public Result<bool> SetStatus(bool enable)
        {
            string command = "";
            Result<bool> result = new Result<bool>();
            AppendInfo(Name, $"设置负载输入{enable}");
            command = enable ? CmdSetInputOn : CmdSetInputOff;
            var re = Write(command);
            if (!re.IsSucceed)
            {
                result.IsSucceed = false;
                result.Err = "设置负载输入失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            Thread.Sleep(100);
            command = CmdReadInput;
            //发送指令并返回
            var re2 = WriteThenRead(command);
            if (!re2.IsSucceed || !re2.Value.Contains(enable ? CmdSetInputOnOk : CmdSetInputOffOk))
            {
                result.IsSucceed = false;
                result.Err = "设置负载输入失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            AppendInfo(Name, "设置负载输入成功");
            return result.EndTime();
        }
        public double VolMin = 0;
        public double VolMax = 120.000f;
        public double CurMin = 0;
        public double CurMax = 20.0000f;
        public double WattMin = 0;
        public double WattMax = 120.000f;
        public double RMin = 0;
        public double RMax = 4000.000f;

        string CmdSetRemote => "SYSTem:REMote\r\n";
        string CmdSetInputOn=> $"INPut ON\r\n";
        string CmdSetInputOff => $"INPut OFF\r\n";
        string CmdReadInput => $"INPut?\r\n";
        string CmdSetInputOnOk => "ON";
        string CmdSetInputOffOk => "OFF";
        string CmdSetFuZaiValue(DianZiFuZaiEnum.FuZaiMode mode, double value)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.FuZaiMode.电压: return $"VOLTage {value}\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电流: return $"CURRent {value}\r\n";
                case DianZiFuZaiEnum.FuZaiMode.功率: return $"POWer {value}\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电阻: return $"RESistance {value}\r\n";
                default:
                    return "";
            }
        }
        string CmdReadFuZaiValue(DianZiFuZaiEnum.FuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.FuZaiMode.电压: return $"VOLTage?\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电流: return $"CURRent?\r\n";
                case DianZiFuZaiEnum.FuZaiMode.功率: return $"POWer?\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电阻: return $"RESistance?\r\n";
                default:
                    return "";
            }
        }
        string CmdSetFuZaiMode(DianZiFuZaiEnum.FuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.FuZaiMode.电压: return "FUNCtion VOLTage\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电流: return "FUNCtion CURRent\r\n";
                case DianZiFuZaiEnum.FuZaiMode.功率: return "FUNCtion POWer\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电阻: return "FUNCtion RESistance\r\n";
                default:
                    return "";
            }
        }
        string CmdReadFuZaiMode => "FUNCtion?\r\n";
        string CmdReadFuZaiModeOk(DianZiFuZaiEnum.FuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.FuZaiMode.电压: return "VOLTage";
                case DianZiFuZaiEnum.FuZaiMode.电流: return "CURRent";
                case DianZiFuZaiEnum.FuZaiMode.功率: return "POWer";
                case DianZiFuZaiEnum.FuZaiMode.电阻: return "RESistance";
                default:
                    return "";
            }
        }
        string CmdReadCurrentValue(DianZiFuZaiEnum.FuZaiMode mode)
        {
            switch (mode)
            {
                case DianZiFuZaiEnum.FuZaiMode.电压: return "MEASure VOLTage\r\n";
                case DianZiFuZaiEnum.FuZaiMode.电流: return "MEASure CURRent\r\n";
                case DianZiFuZaiEnum.FuZaiMode.功率: return "MEASure POWer\r\n";
                default:
                    return "";
            }
        }
    }
}
